package cn.ac.jaas.ljx.wordsegmentation.interfaces.impl;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import cn.ac.jaas.ljx.wordsegmentation.domain.Document;
import cn.ac.jaas.ljx.wordsegmentation.domain.Word;
import cn.ac.jaas.ljx.wordsegmentation.interfaces.Segmenter;
import cn.ac.jaas.ljx.wordsegmentation.util.WordCountUtil;
import org.nlpcn.commons.lang.util.StringUtil;

import com.sun.jna.Library;
import com.sun.jna.Native;

public class NLPIRSegmenter implements Segmenter {

	@Override
	public List<Word> segment(Document document) {
		String argu = "D:\\NLPIR";
		// String system_charset = "GBK";//GBK----0
		//String system_charset = "UTF-8";
		int charset_type = 1;
		
		int init_flag = CLibrary.Instance.NLPIR_Init(argu, charset_type, "0");
		String nativeBytes = null;

		if (0 == init_flag) {
			nativeBytes = CLibrary.Instance.NLPIR_GetLastErrorMsg();
			System.err.println("初始化失败！fail reason is "+nativeBytes);
			return null;
		}

		String sInput = document.getTitle() + document.getDigest();
		//System.out.println(sInput);
		if(StringUtil.isBlank(sInput)) {
			return new ArrayList<Word>();
		}

		//String nativeBytes = null;
		try {
			nativeBytes = CLibrary.Instance.NLPIR_ParagraphProcess(sInput, 1);

			//System.out.println("分词结果为： " + nativeBytes);
			
			String[] strings = nativeBytes.split(" ");
			List<Word> wordsNoCount = new ArrayList<>();
			for(int i=0; i<strings.length; i++) {
				//System.out.println(strings[i]);
				int index = strings[i].indexOf('/');
				if(index <= 0) {
					continue;
				}
				//System.out.println(index);
				String value = strings[i].substring(0, index);
				String natrue = strings[i].substring(index+1);
				Word word = new Word(value, natrue);
				//System.out.println(word.toString());
				wordsNoCount.add(word);
			}
			CLibrary.Instance.NLPIR_Exit();
			List<Word> wordsCounted = new ArrayList<>();
			WordCountUtil.wordCount(wordsNoCount, wordsCounted);
			return wordsCounted;
			/*
			CLibrary.Instance.NLPIR_AddUserWord("网侧壁 n");
			CLibrary.Instance.NLPIR_AddUserWord("底壁 n");
			nativeBytes = CLibrary.Instance.NLPIR_ParagraphProcess(sInput, 1);
			System.out.println("增加用户词典后分词结果为： " + nativeBytes);
			
			CLibrary.Instance.NLPIR_DelUsrWord("要求美方加强对输");
			nativeBytes = CLibrary.Instance.NLPIR_ParagraphProcess(sInput, 1);
			System.out.println("删除用户词典后分词结果为： " + nativeBytes);
			
			
			int nCountKey = 0;
			String nativeByte = CLibrary.Instance.NLPIR_GetKeyWords(sInput, 10,false);

			System.out.print("关键词提取结果是：" + nativeByte);

			nativeByte = CLibrary.Instance.NLPIR_GetFileKeyWords("D:\\NLPIR\\feedback\\huawei\\5341\\5341\\产经广场\\2012\\5\\16766.txt", 10,false);

			System.out.print("关键词提取结果是：" + nativeByte);

			*/

		} catch (Exception ex) {
			// TODO Auto-generated catch block
			ex.printStackTrace();
		}
		return null;
	}
	
	// 定义接口CLibrary，继承自com.sun.jna.Library
	public interface CLibrary extends Library {
		// 定义并初始化接口的静态变量
		CLibrary Instance = (CLibrary) Native.loadLibrary(
				"D:\\win64\\NLPIR", CLibrary.class);

		public int NLPIR_Init(String sDataPath, int encoding,
				String sLicenceCode);
					
		public String NLPIR_ParagraphProcess(String sSrc, int bPOSTagged);

		public String NLPIR_GetKeyWords(String sLine, int nMaxKeyLimit,
				boolean bWeightOut);
		public String NLPIR_GetFileKeyWords(String sLine, int nMaxKeyLimit,
				boolean bWeightOut);
		public int NLPIR_AddUserWord(String sWord);
		public int NLPIR_DelUsrWord(String sWord);
		public String NLPIR_GetLastErrorMsg();
		public void NLPIR_Exit();
		}

	public static String transString(String aidString, String ori_encoding,
			String new_encoding) {
		try {
			return new String(aidString.getBytes(ori_encoding), new_encoding);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

}
